﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Models;

namespace Data
{
    public class CommentsData :DataBase
    {

        public List<Comment> GetComments(int newsId = 0, int authorId = 0, DateTime? publishDate = null, int id = 0)
        {
            var parameters = new List<SqlParameter>
                                 {
                                     new SqlParameter("@NewsID", newsId),
                                     new SqlParameter("@PublishDate", publishDate),
                                     new SqlParameter("@UserID", authorId),
                                     new SqlParameter("@ID", id)
                                 };
            var dataTable = ExecuteDataTable("GetComments", parameters);
            
            if (dataTable.Rows.Count > 0)
            {
                var commentsList = new List<Comment>();
                var dbUsers = new UsersData();
                foreach (DataRow row in dataTable.Rows)
                {
                    commentsList.Add(new Comment
                                         {
                                             Id = Convert.ToInt32(row["ID"]),
                                             NewsId = Convert.ToInt32(row["NewsID"]),
                                             PublishDate = Convert.ToDateTime(row["PublishDate"]),
                                             Text = Convert.ToString(row["Text"]),
                                             Title = Convert.ToString(row["Title"]),
                                             Author = dbUsers.GetUsers(Convert.ToInt32(row["UserID"]))[0]
                                         });
                }

                return commentsList;
            }
            return null;
        }

        public void AddComment(Comment comment)
        {
            var parameters = new List<SqlParameter>
                                 {
                                     new SqlParameter("@NewsID", comment.NewsId),
                                     new SqlParameter("@Title", comment.Title),
                                     new SqlParameter("@Text", comment.Text),
                                     new SqlParameter("@PublishDate", comment.PublishDate),
                                     new SqlParameter("@UserLogin", comment.Author.Login)
                                 };
            ExecuteNonQuery("AddComment", parameters);
        }

        public void EditComment(Comment comment)
        {
            var parameters = new List<SqlParameter>
                                 {
                                     new SqlParameter("@ID", comment.Id),
                                     new SqlParameter("@Title", comment.Title),
                                     new SqlParameter("@Text", comment.Text),
                                 };
            ExecuteNonQuery("EditComment", parameters);
        }
        public void DeleteComment(int id)
        {
            var parameters = new List<SqlParameter> {new SqlParameter("@ID", id)};
            ExecuteNonQuery("DeleteComment", parameters);
        }
    
    }
}